<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Member\ImportAction;
use App\Admin\Actions\Post\BatchRestore;
use App\Country;
use App\Diseases;
use App\SeaArea;
use App\Strain;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Admin\Actions\Post\Restore;

class StrainController extends AdminController
{

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '数据库';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Strain());
        $countrys = Country::withTrashed()->pluck('name','id')->toArray();
        $seas = SeaArea::withTrashed()->pluck('name','id')->toArray();
        $diseases = Diseases::withTrashed()->pluck('name','id')->toArray();

        $grid->column('id', __('Id'));
        $grid->column('country_id', __('国家'))->display(function ()use($countrys){
            return $countrys[$this->country_id]??'';
        });
        $grid->column('sea_area_id', __('海域'))->display(function ()use($seas){
            return $seas[$this->sea_area_id]??'';
        });
        $grid->column('diseases_id', __('疾病种类'))->display(function ()use($diseases){
            return $diseases[$this->diseases_id]??'';
        });
        $grid->column('strain_name', __('菌株名称'));
        $grid->column('characteristics', __('治病特征'));
        $grid->column('drug', __('耐药特征'));
        $grid->column('clinica', __('临床表现'));
        $grid->column('treatment', __('治疗方案'));
        $grid->column('check', __('实验室检查'));
        $grid->column('spread', __('传播途径'));
        $grid->column('score', __('信息来源'));
        $grid->column('remarks', __('备注'));
        $grid->column('created_at', __('创建时间'))->display(function (){
            return ' '.$this->created_at;
        });
        $grid->column('updated_at', __('最新修改时间'))->display(function (){
            return ' '.$this->updated_at;
        });
        $grid->filter(function (Grid\Filter $filter)use($countrys,$seas,$diseases) {

            $filter->disableIdFilter();
            $filter->equal('country_id','国家')->multipleSelect($countrys);
            $filter->equal('sea_area_id','海域')->multipleSelect($seas);
            $filter->equal('diseases_id','疾病种类')->multipleSelect($diseases);
            $filter->like('strain_name', '菌株名称');
            $filter->between('created_at', '创建日期')->date();
            // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
            $filter->scope('trashed', '回收站')->onlyTrashed();

        });
        $grid->actions(function ($actions) {

            if (\request('_scope_') == 'trashed') {
                $actions->add(new Restore());
            }

        });

        $grid->batchActions(function ($batch) {

            if (\request('_scope_') == 'trashed') {
                $batch->add(new BatchRestore());
            }

        });

        // 添加到列表上
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportAction());
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $info = Strain::withTrashed()->find($id);
        $info->country_id = (Country::find($info->country_id))->name??'';
        $info->sea_area_id = (SeaArea::find($info->sea_area_id))->name??'';
        $info->diseases_id = (Diseases::find($info->diseases_id))->name??'';

        $show = new Show($info);

        $show->field('id', __('Id'));

        $show->field('country_id', __('国家'));
        $show->field('sea_area_id', __('海域'));
        $show->field('diseases_id', __('疾病种类'));
        $show->field('strain_name', __('菌株名称'));
        $show->field('characteristics', __('治病特征'));
        $show->field('drug', __('耐药特征'));
        $show->field('clinica', __('临床表现'));
        $show->field('treatment', __('治疗方案'));
        $show->field('check', __('实验室检查'));
        $show->field('spread', __('传播途径'));
        $show->field('score', __('信息来源'));
        $show->field('remarks', __('备注'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('最新修改时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $countrys = Country::pluck('name','id')->toArray();
        $seas = SeaArea::pluck('name','id')->toArray();
        $diseases = Diseases::pluck('name','id')->toArray();
        $form = new Form(new Strain());
        $form->select('country_id','国家')->options($countrys)->required();
        $form->select('sea_area_id','海域')->options($seas)->required();
        $form->select('diseases_id','疾病种类')->options($diseases)->required();
        $form->text('strain_name', __('菌株名称'))->required();
        $form->textarea('characteristics', __('治病特征'));
        $form->textarea('drug', __('耐药特征'));
        $form->textarea('clinica', __('临床表现'));
        $form->textarea('treatment', __('治疗方案'));
        $form->text('check', __('实验室检查'));
        $form->text('spread', __('传播途径'));
        $form->text('score', __('信息来源'));
        $form->text('remarks', __('备注'));

        return $form;
    }
}
